package com.bk.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import com.bk.entity.MenuInfo;
import com.bk.mapper.MenuRoleMapper;
import com.bk.util.ReturnUtil;
import com.bk.util.SerilizableObjectUtil;
import com.bk.vo.MenuIdRoleIdVo;

@Service
public class MenuRoleService {

	@Autowired
	private MenuRoleMapper menuRoleMapper;

	@Autowired /** 对应缓存服务 */
	private RedisMenuGetService redisMenuGetService;

	@Autowired
	private RedisService redisService;

	/** 通过权限id获取对应权限的菜单 */
	public List<MenuInfo> getRoleMenuList() {
		String value = redisService.getValue("authorityId");
		List<MenuInfo> menuList = new ArrayList<MenuInfo>();
		if (value != null) {
			/** 通过tb_role_menu表获取对应的一级菜单id */
			List<Integer> menuIdList = menuRoleMapper.getMenuIdByRoleId(value);/** 通过角色id获取所有可见菜单id */
			if (menuIdList.size() > 0) {
				menuList = menuRoleMapper.getRoleMenuList(menuIdList);
			}
			return menuList;
		} else {
			return menuList;
		}

	}

	/** 获得二级菜单 */
	public List<MenuInfo> getTwoRoleMenuList(int parentId) {
		String value = redisService.getValue("authorityId");
		List<MenuInfo> menuList = new ArrayList<MenuInfo>();
		if (value != null) {
			menuList = menuRoleMapper.getTwoRoleMenuList(parentId);
			return menuList;
		} else {
			return menuList;
		}

	}

	/** 获取所有未授权菜单 */
	public ReturnUtil<MenuInfo> getAllRoleUnMenuList(int roleId) {
		String value = String.valueOf(roleId);
		List<MenuInfo> menuList = null;
		List<Integer> menuIdList1 = null;
		List<Integer> existMenu = menuRoleMapper.getMenuIdByRoleId(value);
		menuIdList1 = menuRoleMapper.getAllRoleUnMenuList1(existMenu);
		menuList = menuRoleMapper.getRoleMenuList(menuIdList1);
		return null != menuList ? new ReturnUtil<MenuInfo>(200, "所有未授权菜单", menuList)
				: new ReturnUtil<MenuInfo>(200, "所有菜单已授权", null);
	}

	/** 获取已授权菜单 */
	public List<MenuInfo> getHaveRoleMenuList(int roleId) {
		String value = String.valueOf(roleId);
		List<MenuInfo> menuList = new ArrayList<MenuInfo>();
		if (value != null) {
			List<Integer> menuIdList = menuRoleMapper.getMenuIdByRoleId(value);/** 通过角色id获取所有可见菜单id */
			menuList = menuRoleMapper.getRoleMenuList(menuIdList);
			return menuList;
		} else {
			return menuList;
		}
	}

	/** 移除菜单权限 */
	public ReturnUtil<MenuInfo> removeAutority(String menuAuths, String roleId) {
		String[] arr = menuAuths.split(","); // 用,分割
		int returnCode = menuRoleMapper.removeAutority(arr, roleId);
		return returnCode > 0 ? new ReturnUtil<MenuInfo>(200, "移除授权菜单成功", null)
				: new ReturnUtil<MenuInfo>(400, "移除授权菜单失败", null);
	}

	/** 添加菜单权限 */
	public ReturnUtil<MenuInfo> addmenuAutority(String menuAuths, String roleId) {

		String[] arr = menuAuths.split(","); // 用,分割
		List<MenuIdRoleIdVo> lmList = new ArrayList<MenuIdRoleIdVo>();
		for (int i = 0; i < arr.length; i++) {
			MenuIdRoleIdVo mv = new MenuIdRoleIdVo(arr[i], roleId);
			lmList.add(mv);
		}
		int returnCode = menuRoleMapper.addmenuAutority(lmList);
		return returnCode > 0 ? new ReturnUtil<MenuInfo>(200, "添加授权菜单成功", null)
				: new ReturnUtil<MenuInfo>(400, "添加授权菜单失败", null);
	}
	
	@PostMapping("/getStatus")/**获取当前会话状态*/
	public ReturnUtil<MenuInfo> getStatus() {
		String value = redisService.getValue("authorityId");
		return value!=""?new ReturnUtil<MenuInfo>(200, "会话仍然存活", null):new ReturnUtil<MenuInfo>(400, "会话已失效", null);
	}

}
